package com.qall.configability.prop;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;


import java.util.Map;

public class InitRemoteConfigService implements EnvironmentPostProcessor, Ordered {

private static final String QALL_CONFIG_PROPERTIES_NAME="qall-config-server";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        Map<String, Object> configApp = GetConfigUtil.getConfig( application);
        if (configApp == null) {
            return;
        }
        RemoteProperties remoteProperties = new RemoteProperties(configApp);
        PropertiesPropertySource propertySource = new PropertiesPropertySource(QALL_CONFIG_PROPERTIES_NAME, remoteProperties.getProperties());
        environment.getPropertySources().addLast(propertySource);
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }
}
